package cn.yun.cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
cookie细节:
cookie在浏览器中保存多长时间
 1.默认情况下,关闭浏览器后 cookie数据被销毁
 2.持久化存储: setMaxAge(int seconds):
              1.参数是正数:将cookie数据写到硬盘的文件中,持久化存储,
                         并指定cookie的存货时间,时间到后,cookie文件自动失效
              2.参数是负数: 关闭浏览器后 cookie数据被销毁
              3.参数是0: 删除cookie信息

*/
@WebServlet("/cookieDemo4")
public class CookieDemo4 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.创建cookie对象
        Cookie cookie1 = new Cookie("msg", "nihao");
        //2.设置cookie的存活时间
        cookie1.setMaxAge(30);//将cookie数据写到硬盘的文件中,30秒后,cookie文件自动失效
        //3.发送cookie
        response.addCookie(cookie1);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
